﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Reflection;
    using System.Windows;
    using PowerSong.Import;
    using PowerSong.SongDatabase;
    using PowerSong.UserInterface.Tasks;

    /// <summary>
    /// Interaction logic for WelcomeWindow.xaml
    /// </summary>
    public partial class WelcomeWindow : Window
        {

        public WelcomeWindow()
            {
            InitializeComponent();
            var version = Assembly.GetExecutingAssembly().GetName().Version;
            this.TitleTextBlock.Text = string.Format("PowerSong {0}.{1}", version.Major, version.Minor);
            this.BuildTextBlock.Text = string.Format("Build {0}", version.Revision);
            }

        private void CreateDatabaseButton_Click(object sender, RoutedEventArgs e)
            {

            // Hide this form while asking for a file name
            this.Topmost = false;
            var databaseCreated = CreateDatabase();
            this.Topmost = true;
            if (databaseCreated == true)
                {

                // Close the window
                this.DialogResult = true;

                }

            }

        private void LoadDatabaseButton_Click(object sender, RoutedEventArgs e)
            {
            if (LoadDatabase()) this.DialogResult = true;
            }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        public static bool CreateDatabase()
            {

            // Show the "create database" dialog
            var window = new CreateDatabaseWindow();
            var result = window.ShowDialog();

            // Was a file name specified?
            if (result == true)
                {

                try
                    {

                    // Create the database with default settings
                    State.CreateDatabase(window.FileName);
                    State.Settings.AutoCreateProjectionWindow = true;
                    State.Settings.PopupSongPreviews = true;

                    // Does the user want to record song usage?
                    if (window.OptionRecordUsage) State.Settings.EnableReporting = true;

                    // Save changes to settings
                    State.Settings.SaveChanges();

                    // Download and import the database from the Internet if required
                    if (window.OptionDownloadSongs || window.OptionDownloadStyles)
                        {
                        var importer = new WebsiteImport(State.Database,
                                                         window.OptionDownloadSongs,
                                                         window.OptionDownloadStyles,
                                                         false);
                        var task = new ImportTask(importer, "Downloaded from Website", "Downloading data from website and loading it into the database...");
                        ProgressWindow.Start(task);

                        // Rebuild the song index
                        ProgressWindow.Start(new RecreateIndexTask());

                        // Show messages if any were generated
                        if (task.Messages.Length > 0)
                            {
                            MessagesWindow.Show("The following issues were encountered with the data downloaded from the website, which may have affected the import into the database. Please review these messages before continuing.", task.Messages);
                            }

                        }

                    // Everything was successful
                    return true;

                    }
                catch (Exception ex)
                    {
                    Utilities.ShowErrorMessage("Cannot Create Database", ex.Message);
                    return false;
                    }

                }
            
            return false;

            }

        public static bool LoadDatabase()
            {

            // Ask for the file name
            var fileName = OpenFileControl.RequestFileName("PowerSong Database|*.PowerSong|All Files|*.*");
            if (fileName != null)
                {

                try
                    {
                    return State.LoadDatabase(fileName);
                    }
                catch (Exception ex)
                    {
                    Utilities.ShowErrorMessage("Cannot Load Database", ex.Message);
                    return false;
                    }

                }

            return false;

            }

        private void ConvertDatabaseButton_Click(object sender, RoutedEventArgs e)
            {

            // Show the window
            this.Topmost = false;
            var window = new ConvertOldDatabaseWindow();
            var result = window.ShowDialog();
            this.Topmost = true;

            // Did the user specify that the database should be immediately used?
            if (result == true && !string.IsNullOrEmpty(window.TargetFileName))
                {
                State.LoadDatabase(window.TargetFileName);

                // Close the window
                this.DialogResult = true;
                }

            }

        }

    }
